/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import dao.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import pojo.Departments;
import pojo.Jobs;

/**
 *
 * @author A7med Kotb
 */
public class JobsJpaController implements Serializable {

    public JobsJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Jobs jobs) throws RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Departments deptName = jobs.getDeptName();
            if (deptName != null) {
                deptName = em.getReference(deptName.getClass(), deptName.getDId());
                jobs.setDeptName(deptName);
            }
            em.persist(jobs);
            if (deptName != null) {
                deptName.getJobsCollection().add(jobs);
                deptName = em.merge(deptName);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Jobs jobs) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Jobs persistentJobs = em.find(Jobs.class, jobs.getJId());
            Departments deptNameOld = persistentJobs.getDeptName();
            Departments deptNameNew = jobs.getDeptName();
            if (deptNameNew != null) {
                deptNameNew = em.getReference(deptNameNew.getClass(), deptNameNew.getDId());
                jobs.setDeptName(deptNameNew);
            }
            jobs = em.merge(jobs);
            if (deptNameOld != null && !deptNameOld.equals(deptNameNew)) {
                deptNameOld.getJobsCollection().remove(jobs);
                deptNameOld = em.merge(deptNameOld);
            }
            if (deptNameNew != null && !deptNameNew.equals(deptNameOld)) {
                deptNameNew.getJobsCollection().add(jobs);
                deptNameNew = em.merge(deptNameNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = jobs.getJId();
                if (findJobs(id) == null) {
                    throw new NonexistentEntityException("The jobs with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Jobs jobs;
            try {
                jobs = em.getReference(Jobs.class, id);
                jobs.getJId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The jobs with id " + id + " no longer exists.", enfe);
            }
            Departments deptName = jobs.getDeptName();
            if (deptName != null) {
                deptName.getJobsCollection().remove(jobs);
                deptName = em.merge(deptName);
            }
            em.remove(jobs);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Jobs> findJobsEntities() {
        return findJobsEntities(true, -1, -1);
    }

    public List<Jobs> findJobsEntities(int maxResults, int firstResult) {
        return findJobsEntities(false, maxResults, firstResult);
    }

    private List<Jobs> findJobsEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Jobs.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Jobs findJobs(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Jobs.class, id);
        } finally {
            em.close();
        }
    }

    public int getJobsCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Jobs> rt = cq.from(Jobs.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
